<?php
namespace app\admin\service;

use app\model\RatingScaleCloudModel;
use app\model\RatingScaleDownloadLogModel;
use app\model\RatingScaleFactorLevelModel;
use app\model\RatingScaleFactorModel;
use app\model\RatingScaleModel;
use app\model\RatingScaleFactorWarningModel;
use app\model\RatingScaleModeModel;
use app\model\RatingScaleQuestionAnswerModel;
use app\model\RatingScaleQuestionModel;
use think\Service;

class RatingScaleCloudService extends Service
{
    public string $message;
    public int $line;
    /**
     * 下载
     * @param int $CloudId 云端列表id
     * @param array $userData 用户信息
     */
    public function download(int $CloudId, array $userData): bool
    {
        try {
            $RatingScaleCloud  = new RatingScaleCloudModel();
            $info = $RatingScaleCloud->find($CloudId);
            //复制量表
            $RatingScale = new RatingScaleModel();
            $RatingScaleData = $RatingScale->field('name,previous_name,type_id,cover,subject_time,answer_time,read_time,times_time,use_price,interpret_price,introduce,notice')->find($info->channel_id)->toArray();
            $RatingScaleData['unit_id'] = $userData['unit_id'];
            $RatingScaleData['uid'] = $userData['id'];
            $RatingScaleData['name'] = $info['name'];
            $RatingScaleData['channel'] = 4;
            $RatingScaleData['time'] = time();
            $RatingScaleId = $RatingScale->insertGetId($RatingScaleData);
            //model复制
            $RatingScaleMode = new RatingScaleModeModel();
            $RatingScaleModeList = $RatingScaleMode->where('rating_scale_id','=',$info->rating_scale_id)->select()->toArray();
            foreach ($RatingScaleModeList as $item) {
                $modeOldToNewId[$item['id']] = $RatingScaleMode->insertGetId([
                    'name' => $item['name'],
                    'rating_scale_id' => $RatingScaleId,
                    'is_default' => $item['is_default'],
                ]);
            }
            //因子复制
            $RatingScaleFactor = new RatingScaleFactorModel();
            $RatingScaleFactorList = $RatingScaleFactor->where('rating_scale_id','=',$info->rating_scale_id)->select()->toArray();
            foreach ($RatingScaleFactorList as $item) {
                $item['rating_scale_id'] = $RatingScaleId;
                $item['mode_id'] = $modeOldToNewId[$item['mode_id']];
                $oldId = $item['id'];
                unset($item['id']);
                $factorOldToNewId[$oldId] = $RatingScaleFactor->insertGetId($item);
            }
            //复制factor level表
            $RatingScaleFactorLevelData = [];
            $RatingScaleFactorLevel = new RatingScaleFactorLevelModel();
            $RatingScaleFactorLevelList = $RatingScaleFactorLevel->where('rating_scale_id','=',$info->rating_scale_id)->select()->toArray();
            if (count($RatingScaleFactorLevelList) > 0) {
                foreach ($RatingScaleFactorLevelList as $item) {
                    $item['rating_scale_id'] = $RatingScaleId;
                    $item['mode_id'] = $modeOldToNewId[$item['mode_id']];
                    $item['factor_id'] = $factorOldToNewId[$item['factor_id']];
                    unset($item['id']);
                    $RatingScaleFactorLevelData[] = $item;
                }
                $RatingScaleFactorLevel->insertAll($RatingScaleFactorLevelData);
            }
            //插入factor warning表
            $RatingScaleFactorWarningData = [];
            $RatingScaleFactorWarning = new RatingScaleFactorWarningModel();
            $RatingScaleFactorWarningList = $RatingScaleFactorWarning->where('rating_scale_id','=',$info->rating_scale_id)->select()->toArray();
            if (count($RatingScaleFactorWarningList) > 0) {
                foreach ($RatingScaleFactorWarningList as $item) {
                    $item['rating_scale_id'] = $RatingScaleId;
                    $item['mode_id'] = $modeOldToNewId[$item['mode_id']];
                    $item['factor_id'] = $factorOldToNewId[$item['factor_id']];
                    unset($item['id']);
                    $RatingScaleFactorWarningData[] = $item;
                }
                $RatingScaleFactorWarning->insertAll($RatingScaleFactorWarningData);
            }
            //复制题目
            $RatingScaleQuestion = new RatingScaleQuestionModel();
            $RatingScaleQuestionList = $RatingScaleQuestion->where('rating_scale_id','=',$info->rating_scale_id)->select()->toArray();
            $RatingScaleQuestionIds = array_column($RatingScaleQuestionList,'id');
            //答案
            $RatingScaleQuestionAnswerData = [];
            $RatingScaleQuestionAnswer = new RatingScaleQuestionAnswerModel();
            $RatingScaleQuestionAnswerList = $RatingScaleQuestionAnswer->where('question_id','in',$RatingScaleQuestionIds)->select()->toArray();
            foreach ($RatingScaleQuestionList as $item){
                $itemQuestionId = $RatingScaleQuestion->insertGetId([
                    'rating_scale_id' => $RatingScaleId,
                    'num' => $item['num'],
                    'name' => $item['name'],
                    'type' => $item['type']
                ]);
                foreach ($RatingScaleQuestionAnswerList as $itemAnswer){
                    $RatingScaleQuestionAnswerData[] = [
                        'question_id' => $itemQuestionId,
                        'name' => $itemAnswer['name'],
                        'score' => $itemAnswer['score'],
                    ];
                }
            }
            if (count($RatingScaleQuestionAnswerData) > 0){
                $RatingScaleQuestionAnswer->insertAll($RatingScaleQuestionAnswerData);
            }
            $RatingScaleDownloadLog = new RatingScaleDownloadLogModel();
            $RatingScaleDownloadLog->insert([
                'unit_id' => $userData['unit_id'],
                'uid' => $userData['id'],
                'cloud_id'  => $CloudId,
                'rating_scale_id' => $RatingScaleId,
                'time' => time()
            ]);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }


}